package com.dmall.ssm.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.dmall.ssm.dao.UserDao;
import com.dmall.ssm.pojo.User;
import com.dmall.ssm.service.TestRedisService;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.Transaction;

///@Service
public class TestRedisServiceImpl implements TestRedisService {

	//@Autowired
	private UserDao userDao;
	
	//@Autowired
	private JedisPool jedisPool;
	
	@Override
	//@Transactional
	public void execute(User user) {
		String json = JSON.toJSONString(user);
		Jedis jedis = jedisPool.getResource();
		Transaction transaction = jedis.multi();
		try {
			// 1.将user对象转换为json写入到redis中
			transaction.set("user:"+user.getId(), json);
			// 2.将userids中对应的user删掉
			transaction.srem("unloadids", user.getId()+"");
			//int i=1/0;
			// 3.将userid加入到另外的一个集合中
			transaction.sadd("unpushids", user.getId()+"");
			transaction.exec();
		} catch (Exception e) {
			transaction.discard();
			throw new RuntimeException(e.getMessage());
		}
		// 4.修改user对象的real_name
		userDao.updateRealName(user);
	}

	@Override
	//@Transactional
	public void testTx() {
		User user1 = new User();
		user1.setId(1);
		user1.setRealName("张三三");
		
		User user2 = new User();
		user2.setId(2);
		user2.setRealName("李思思");
		
		userDao.updateRealName(user1);
		int i=1/0;
		userDao.updateRealName(user2);
	}
}
